﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NPMonitor.Implementation;
using NPMonitor.DataModel.Report;
using NPMonitor.Web.UserControls;
using System.Text.RegularExpressions;
using System.IO;

namespace NPMonitor.Web
{
    public partial class ReportView : BasePage
    {
        public override string FunctionName
        {
            get { return FunctionEnum.Reports.ToString(); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            rfvJobName.ErrorMessage = string.Format(ValidationString.PleaseSelect, "Job");
            cvScenarioNO.ErrorMessage = string.Format(ValidationString.PleaseSelect, "Scenario");
            if (!IsPostBack)
            {
                BindJobName();
                ddlJob.SelectedIndex = 0;
                BindChkLocation();
                hfReportType.Value = reportType.ToString();
                switch (reportType)
                {
                    case ReportType.Daily:
                        rtpTimeSelector.EndDate = DateTime.UtcNow.ToDateHourString();
                        rtpTimeSelector.BeginDate = DateTime.UtcNow.GetBeginTimeOfDailyReport().ToDateHourString();
                        break;
                    case ReportType.Hourly:
                        rtpTimeSelector.EndDate = DateTime.UtcNow.ToDateHourString();
                        rtpTimeSelector.BeginDate = DateTime.UtcNow.GetBeginTimeOfHourlyReport().ToDateHourString();
                        break;
                    case ReportType.Weekly:
                        rtpTimeSelector.EndDate = DateTime.UtcNow.ToDateString();
                        rtpTimeSelector.BeginDate = DateTime.UtcNow.GetFirstDayOfThisWeek().ToDateString();
                        break;
                    case ReportType.Monthly:
                        rtpTimeSelector.EndDate = DateTime.UtcNow.ToDateString();
                        rtpTimeSelector.BeginDate = DateTime.UtcNow.GetFirstDayOfThisMonth().ToDateString();
                        break;
                    case ReportType.Yearly:
                        DateTime time = DateTime.UtcNow;
                        rtpTimeSelector.EndDate = time.ToDateString();
                        rtpTimeSelector.BeginDate = time.GetFirstMonthOfThisYear().ToDateString();
                        break;
                }
            }

        }

        public ReportType reportType
        {
            get
            {
                ReportType reporttype = ReportType.Daily;
                var type = Request.QueryString["ReportType"];
                Enum.TryParse<ReportType>(type, out reporttype);
                return reporttype;
            }
        }

        protected void BindChkLocation()
        {
            var locationList = StoreManager.Current.MachineStore.GetAllMachinePool();
            chkLocation.DataSource = locationList;
            chkLocation.DataTextField = "PoolName";
            chkLocation.DataValueField = "PoolId";
            chkLocation.DataBind();
            for (int i = 0; i < chkLocation.Items.Count; i++)
            {
                chkLocation.Items[i].Selected = true;
            }
        }

        protected void btnViewReport_Click(object sender, EventArgs e)
        {
            BindData();
        }

        protected void BindJobName()
        {
            var jobList = StoreManager.Current.JobDefinitionStore.GetAllJobDefinitions(0, -1);
            ddlJob.BindDataSource(jobList, "JobName", "JobId");
        }

        private void BindData()
        {
            var beginTime = rtpTimeSelector.BeginDate;
            var endTime = rtpTimeSelector.EndDate;
            string reportPath=@"Reports\rdlc\Report.rdlc";
            var reportTime = string.Format("{0} - {1}" ,beginTime.ToReportDatetimeFormat(),endTime.ToReportDatetimeFormat());
            var reportDateTime = string.Format("{0} - {1}", beginTime.ToReportDateFormat(), endTime.ToReportDateFormat());
            var jobName = ddlJob.SelectedItem.Text;
            string scenarioNo = hfScenarioNO.Value;
            var scenarioName = StoreManager.Current.JobDefinitionStore.GetScenarioByNo(scenarioNo).ScenarioName;
            var reportTitle = string.Format("{0}({1} - {2})", "DSM", jobName, scenarioName);
            if (beginTime != "" && endTime != "")
            {
                TimeSpan timeInterval = endTime.ToDateTimeByFormat().Value - beginTime.ToDateTimeByFormat().Value;
                List<ReportData> datalist;
                List<string> locationList = new List<string>();
                for (int i = 0; i < chkLocation.Items.Count; i++)
                {
                    if (chkLocation.Items[i].Selected)
                    {
                        locationList.Add(chkLocation.Items[i].Value);
                    }
                }
                var baseLine = StoreManager.Current.ReportStore.GetBaseLine(scenarioNo);
                switch (reportType)
                {
                    case ReportType.Hourly:
                            datalist = StoreManager.Current.ReportStore.GetHourlyReportDataByCondition(scenarioNo, beginTime, endTime, locationList.ToArray());
                            ucReport.BindReportData(datalist, baseLine, reportPath, reportTime, reportTitle);
                        break;
                    case ReportType.Daily:
                        datalist = StoreManager.Current.ReportStore.GetDailyReportDataByCondition(scenarioNo, beginTime, endTime, locationList.ToArray());
                        ucReport.BindReportData(datalist, baseLine, reportPath, reportTime, reportTitle);
                        break;
                    case ReportType.Weekly:
                        datalist = StoreManager.Current.ReportStore.GetWeeklyMonthlyReportDataByCondition(scenarioNo, beginTime, endTime, locationList.ToArray());
                        ucReport.BindReportData(datalist, baseLine, reportPath, reportDateTime, reportTitle);
                        break;
                    case ReportType.Monthly:
                        datalist = StoreManager.Current.ReportStore.GetWeeklyMonthlyReportDataByCondition(scenarioNo, beginTime, endTime, locationList.ToArray());
                        ucReport.BindReportData(datalist, baseLine, reportPath, reportDateTime, reportTitle);
                        break;
                    case ReportType.Yearly:
                        datalist = StoreManager.Current.ReportStore.GetYearlyReportDataByCondition(scenarioNo, beginTime, endTime, locationList.ToArray());
                        ucReport.BindReportData(datalist, baseLine, reportPath, reportDateTime, reportTitle);
                        break;
                    case ReportType.HttpRequests:
                        datalist = StoreManager.Current.ReportStore.GetDailyReportDataByCondition(scenarioNo, beginTime, endTime, locationList.ToArray());
                        break;
                    default:
                        datalist = new List<ReportData>();
                        break;
                }
            }
        }
    }
}